<template>
  <div>
    <div>
      <TopFilter ref="formPanel" :list="formList" :cols="4" labelWidth="80" @filterChange="formchangeFun" @resetChange="resetChange" />
      <!-- <ButtonArea :style="{ paddingLeft: '300px' }">
        <el-button type="primary" @click="FormPanelSeacher">搜 索</el-button>
        <el-button @click="FormPanelreset">重置</el-button>
      </ButtonArea> -->
      <FilterTable
        ref="customerTable"
        columnsRef="partPartInventoryAdjustInventorySearchCustomerPage"
        :columns="columnsList"
        :fetchapi="getTableData"
        :dataSource="dataSource"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columnsList = columns)"
        :height="'200'"
        selectionType="single"
        :onRowSelectChange="selectRow"
      ></FilterTable>
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog(1)">取 消</el-button>
      <el-button type="primary" @click="closeDialog(2)">确 认</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { region } from '@/api/cms-common/region'; //行政区域
import { getOwnerCode } from '@/assets/js/auth';
import { byAllInfo, getOrgList } from '@/api/part/allocation';
import { getRegion } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
import { notifyAction } from '../../../../utils';

export default {
  name: 'searchInOrderPage',
  mixins: [dictionary, region],
  props: ['customerNo'],
  data() {
    return {
      formList: this.createFormList(),
      columnsList: this.createTableColumns(),
      getTableData: byAllInfo,
      selectRowData: {},
      fetchParams: {
        customerCode: this.customerNo,
        xhrAbort: true
      },
      dataSource: [],
      orgList: [],
      regionList: []
    };
  },
  created() {},
  mounted() {
    this.getlist();
    this.formList[0].initialValue = this.customerNo;
    // this.getDeptData();
    this.getProvine();
  },
  methods: {
    changeCity(val) {
      console.log('provinChangeVal', val);
      this.formList.find(x => x.fieldName == 'cityId').itemList = this.createSubRegionList('10001002');
    },
    changeCounty(val) {
      console.log('cityChangeVal', val);
      this.formList.find(x => x.fieldName == 'districtId').itemList = this.createSubRegionList('10001003');
    },
    createFormList() {
      return [
        { type: 'INPUT', label: '服务中心代码', fieldName: 'dealerCode', disabled: false },
        { type: 'INPUT', label: '服务中心全称', fieldName: 'dealerName', disabled: false },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '服务中心代码', dataIndex: 'dealerCode', sorter: true, showOverflowTooltip: true, filter: false, filterType: 'input' },
        { title: '服务中心名称', dataIndex: 'dealerShortname', width: 150, showOverflowTooltip: true, sorter: true, filter: false, filterType: 'input' },

        {
          title: '省份',
          dataIndex: 'provinceId',
          sorter: true,
          dictItems: this.createRegionList(),
          filterType: 'checkbox'
        },
        {
          title: '城市',
          dataIndex: 'cityId',
          sorter: true,
          filterType: 'checkbox',
          dictItems: this.regionList
        },
        {
          title: '区县',
          dataIndex: 'districtId',
          sorter: true,
          filterType: 'checkbox',
          dictItems: this.regionList
        }
        // { title: '加价率', dataIndex: 'PRICE_ADD_RATE', sorter: true, filter: false, filterType: 'input' },
        // { title: '订货周期', dataIndex: 'LEAD_TIME', sorter: true, filter: false, filterType: 'input' },
        // { title: '经销商', dataIndex: 'DEALER_NAME', sorter: true, filter: false, filterType: 'input' }
      ];
    },
    // 获取数据字典数据
    async getlist() {
      this.initProvince();
      console.log("this.createSubRegionList('10001002');", this.createSubRegionList('10001002'));
      // this.columnsList.find(x => x.dataIndex == 'deptId').itemList = this.createSubRegionList(10001002);
    },
    // 重置
    FormPanelreset() {
      this.$refs.formPanel.RESET_FORM();
    },
    FormPanelSeacher() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    formchangeFun(val) {
      this.fetchParams.xhrAbort = true;
      // if ((!val.dealerCode && !val.dealerName && !val.provinceId) || (val.dealerCode == '' && val.dealerName == '' && val.provinceId == '')) {
      //   this.$notify.warning({ title: '提示信息', message: '服务中心代码,服务中心全称至少填一个' });
      //   return;
      // }
      this.fetchParams = Object.assign({}, this.fetchParams, val, { xhrAbort: false });
    },
    selectRow(row) {
      console.log(row);
      this.selectRowData = row;
    },
    confirmFun() {},
    async closeDialog(param) {
      if (param == 1) {
        this.$emit('close', false, param);
      } else {
        if (!this.selectRowData.length) {
          notifyAction('请选中数据', 'error');
          return;
        }
        if (this.selectRowData[0].dealerCode == (await getOwnerCode())) {
          notifyAction('不能选择当前服务中心', 'error');
          return;
        }
        this.$emit('addCustomerCode', this.selectRowData);
        this.closeDialog(1);
      }
    },
    //加载事业部数据
    // async getDeptData() {
    //   const res = await getOrgByOrgType('15061005');
    //   if (res.resultCode === 200) {
    //     console.log(res, 'getDeptData::');
    //     this.columnsList.find(item => item.dataIndex === 'deptId').dictItems = res.data.map(x => ({ value: x.orgId, text: x.orgName }));
    //   }
    // },
    //初始化所在省份
    async initProvince() {
      let pl = await this.createRegionList();
      this.formList.find(item => item.fieldName === 'provinceId').itemList = pl;
    },
    //初始化所在市数据
    async initCity(provinceId) {
      if (!provinceId || provinceId == null || typeof provinceId == 'undefined') {
        return;
      }
      //取省数据
      let rd = this.getRegionData();
      console.log(rd, 'rd:::');
      this.cityData = rd[provinceId].children;
      let cl = this.createSubRegionList(this.cityData);
      //初始化市，清除原值
      let city = this.formList.find(item => item.fieldName === 'cityId');
      city.itemList = cl;
      city.initialValue = '';
      //清除区县
      let district = this.formList.find(item => item.fieldName === 'districtId');
      district.itemList = [];
      district.initialValue = '';
    },
    //初始化所在区数据
    async initDistrict(cityId) {
      if (!cityId || cityId == null || typeof cityId == 'undefined') {
        return;
      }
      let cl = this.district(cityId);
      //初始化区县，清除原值
      let district = this.formList.find(item => item.fieldName === 'districtId');
      district.itemList = cl;
      district.initialValue = '';
    },
    resetChange() {
      this.dataSource = [];
    },
    async getProvine() {
      const allRegion = await getRegion();
      const orgList = await getOrgList();
      console.log('orgList', orgList);
      this.regionList = allRegion.data.map(x => ({ value: x.regionId, text: x.regionName }));
      this.columnsList.find(x => x.dataIndex == 'districtId').dictItems = this.regionList;
      this.columnsList.find(x => x.dataIndex == 'cityId').dictItems = this.regionList;
      this.columnsList.find(x => x.dataIndex == 'deptId').dictItems = orgList.data.map(x => ({ text: x.orgName, value: x.orgId }));
      console.log('this.regionList', this.regionList);
      console.log('allRegion', allRegion);
    }
  }
};
</script>

<style lang="less" scoped></style>
